<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>主要内容区main</title>
<link href="../include/css/css.css" type="text/css" rel="stylesheet" />
<link href="../include/css/main.css" type="text/css" rel="stylesheet" />
<link rel="shortcut icon" href="../include/images/main/favicon.ico" />
<style>
body{overflow-x:hidden; background:#f2f0f5; padding:15px 0px 10px 5px;}
#searchmain{ font-size:12px;}
#search{ font-size:12px; background:#548fc9; margin:10px 10px 0 0; display:inline; width:100%; color:#FFF; float:left}
#search form span{height:40px; line-height:40px; padding:0 0px 0 10px; float:left;}
#search form input.text-word{height:24px; line-height:24px; width:180px; margin:8px 0 6px 0; padding:0 0px 0 10px; float:left; border:1px solid #FFF;}
#search form input.text-but{height:24px; line-height:24px; width:55px; background:url(../include/images/main/list_input.jpg) no-repeat left top; border:none; cursor:pointer; font-family:"Microsoft YaHei","Tahoma","Arial",'宋体'; color:#666; float:left; margin:8px 0 0 6px; display:inline;}
#search a.add{ background:url(../include/images/main/add.jpg) no-repeat -3px 7px #548fc9; padding:0 10px 0 26px; height:40px; line-height:40px; font-size:14px; font-weight:bold; color:#FFF; float:right}
#search a:hover.add{ text-decoration:underline; color:#d2e9ff;}
#main-tab{ border:1px solid #eaeaea; background:#FFF; font-size:12px;}
#main-tab th{ font-size:12px; background:url(../include/images/main/list_bg.jpg) repeat-x; height:32px; line-height:32px;}
#main-tab td{ font-size:12px; line-height:40px;}
#main-tab td a{ font-size:12px; color:#548fc9;}
#main-tab td a:hover{color:#565656; text-decoration:underline;}
.bordertop{ border-top:1px solid #ebebeb}
.borderright{ border-right:1px solid #ebebeb}
.borderbottom{ border-bottom:1px solid #ebebeb}
.borderleft{ border-left:1px solid #ebebeb}
.gray{ color:#dbdbdb;}
td.fenye{ padding:10px 0 0 0; text-align:right;}
.bggray{ background:#f9f9f9}
</style>
</head>
<body>
<!--main_top-->
<table width="99%" border="0" cellspacing="0" cellpadding="0" id="searchmain">
  <tr>
    <td width="99%" align="left" valign="top">您的位置：用户管理</td>
  </tr>
  <tr>
    <td align="left" valign="top">
    <table width="100%" border="0" cellspacing="0" cellpadding="0" id="search">
  		<tr>
   		 <td width="90%" align="left" valign="middle">
	         <form action = "index.php">
				 <span>用户：</span>
				 <select name="sex" style="margin:10px 0px">
					<option value="">-请选择-</option>
					<option value="1">-男-</option>
					<option value="2">-女-</option>
				 </select>
				 <input type="submit" value="搜索">
			 </form>
         </td>
  		  <td width="10%" align="center" valign="middle" style="text-align:right; width:150px;"><a href="add.php" target="mainFrame" onFocus="this.blur()" class="add">新增用户</a></td>
  		</tr>
	</table>
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
    
    <table width="100%" border="0" cellspacing="0" cellpadding="0" id="main-tab">
      <tr>
        <th align="center" valign="middle" class="borderright">会员ID</th>
        <th align="center" valign="middle" class="borderright">会员账号</th>
        <th align="center" valign="middle" class="borderright">会员真实姓名</th>
        <th align="center" valign="middle" class="borderright">密码</th>
        <th align="center" valign="middle" class="borderright">性别</th>
        <th align="center" valign="middle" class="borderright">地址</th>
        <th align="center" valign="middle" class="borderright">邮编</th>
        <th align="center" valign="middle" class="borderright">电话</th>
        <th align="center" valign="middle" class="borderright">Email</th>
        <th align="center" valign="middle" class="borderright">状态</th>
        <th align="center" valign="middle" class="borderright">注册时间</th>
        <th align="center" valign="middle">操作</th>
      </tr>
     <?php
			//六脉神剑	
			date_default_timezone_set("PRC");
			$sex = array("1"=>"男","2"=>"女");
			$state = array("3"=>"启用","4"=>"禁用","5"=>"后台管理员");
			include("dbconfig.php");
			$link = @mysqli_connect(HOST,USER,PASS,DBNAME) or die("数据库连接失败");
			mysqli_set_charset($link,"utf8");
			//=================封装条件==================
			//1 设置数组接收搜索条件
			$wherelist = array();
			$where = "";
			$url = '';
			//2 接收搜索条件 
			if(!empty($_GET['name'])){
				$wherelist[] =" name like '%{$_GET['name']}%'"; 
				$urllist[] = "name={$_GET['name']}";
			}	

			if(!empty($_GET['sex'])){
				$wherelist[] = "sex='{$_GET['sex']}'";
				$urllist[] = "sex={$_GET['sex']}";
			}

			//3 判断搜索条件的有效性	
			if(count($wherelist)>0){
				$where = " where ".implode(" and ",$wherelist);
				$url = "&".implode("&",$urllist);
			}
			
			//==================封装条件=================
			//====================实现分页============================
			//1 设置参数 
				// if($_GET['p']<1){
				// 	$page = 1;
				// }
				
				$page =empty($_GET['p'])?  1 : $_GET['p'];//接收url传递的页码值 p=
				$maxRow = 0;//一共有多少条 
				$maxPage = 0;//一共能显示多少页 
				$pageSize = 4;//每页显示多少条 
			//2 确认一共显示多少页 $maxRow
				$sql = "select * from users";
				$result = mysqli_query($link,$sql);
				$maxRow = mysqli_num_rows($result);

			//3 一共能显示多少页 $maxPage
				$maxPage = ceil($maxRow/$pageSize);//17/4 进位取整 ceil进位取整函数

			//4 判断页码 
				if($page>$maxPage){
					$page = $maxPage;
				}
				if($page<1){
					$_GET['p']=1;
				}

			//5 拼接分页的limit条件 limit(页码-1)*页大小,页大小
			$limit = " limit ".($page-1)*$pageSize.",".$pageSize;//sql语句注意空格的问题 
			// echo $limit;
			// exit;

			//====================实现分页============================
			$sql = "select * from users".$where.$limit;
			$result = mysqli_query($link,$sql);
			while($row = mysqli_fetch_assoc($result)){				
				echo "<tr>";
				echo "<td align=center>{$row['id']}</td>";
				echo "<td align=center>{$row['username']}</td>";
				echo "<td align=center>{$row['name']}</td>";
				echo "<td align=center>{$row['pass']}</td>";
				echo "<td align=center>{$sex[$row['sex']]}</td>";
				echo "<td align=center>{$row['address']}</td>";
				echo "<td align=center>{$row['code']}</td>";
				echo "<td align=center>{$row['phone']}</td>";
				echo "<td align=center>{$row['email']}</td>";
				echo "<td align=center>".$state[$row['state']]."</td>";
				echo "<td align=center>".date('Y-m-d H:i:s',$row['addtime'])."</td>";
				
				echo "<td align=center>
					<a href='action.php?a=del&id={$row['id']}'>删除</a>  
					<a href='edit.php?id={$row['id']}'>修改</a>  
					</td>";
				echo "</tr>";
			}
			
		?>
     
    </table></td>
    </tr>
  <?php
     echo "<tr>
        <td align='left' valign='top' class='fenye'>{$maxRow} 条数据 {$page}/{$maxPage} 页&nbsp;&nbsp;
        <a href='index.php?p=1{$url}' target='mainFrame' onFocus='this.blur()'>首页</a>&nbsp;&nbsp;
        <a href='index.php?p=".($page-1)."{$url}' target='mainFrame' onFocus='this.blur()'>上一页</a>&nbsp;&nbsp;
        <a href='index.php?p=".($page+1)."{$url}' target='mainFrame' onFocus='this.blur()'>下一页</a>&nbsp;&nbsp;
        <a href='index.php?p={$maxPage}{$url}' target='mainFrame' onFocus='this.blur()'>尾页</a>
        </td>
      </tr>"
    ?>

</table>
</body>
</html>